package com.dqhl.community.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.dqhl.community.R;
import com.dqhl.community.adapter.ExpressListAdapter;
import com.dqhl.community.base.BaseActivity;
import com.dqhl.community.model.ExpressReceive;
import com.dqhl.community.util.Config;
import com.dqhl.community.util.Dlog;
import com.dqhl.community.util.JsonUtils;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.api.GoogleApiClient;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.List;

/**
 * Created by LIENLIN on 2016/8/29.
 * 快递代收
 */
public class ExpressReceiveActivity extends BaseActivity implements View.OnClickListener {

    private Context context;
    private ImageView ivTopBack;
    private LinearLayout llExpressName;
    private TextView tvExpressName;
    private ImageView ivArrow;
    private RelativeLayout rlTop;
    private ExpressListAdapter expressListAdapter;
    private List<ExpressReceive> expressReceiveList;
    private EditText et_order_number;
    private Button btn_search;
    private ListView lv_expressReceibver;
    private TextView tv_no_info;

    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    private GoogleApiClient client;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = this;
        setContentView(R.layout.activity_express_receive);
        initView();
        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
    }

    private void initView() {
        rlTop = (RelativeLayout) findViewById(R.id.rl_top);
        ivTopBack = (ImageView) findViewById(R.id.iv_top_back);
        llExpressName = (LinearLayout) findViewById(R.id.ll_express_name);
        tvExpressName = (TextView) findViewById(R.id.tv_express_name);
//        ivArrow = (ImageView) findViewById(R.id.iv_arrow);
        et_order_number = (EditText) findViewById(R.id.et_order_number);
        lv_expressReceibver = (ListView) findViewById(R.id.lv_express);
        btn_search = (Button) findViewById(R.id.btn_search);
        tv_no_info = (TextView) findViewById(R.id.tv_no_info);

        et_order_number.setOnClickListener(this);
        btn_search.setOnClickListener(this);
        ivTopBack.setOnClickListener(this);
        llExpressName.setOnClickListener(this);
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_top_back:
                finish();
                break;
            case R.id.btn_search:
                getData();
                break;
        }
    }

    //获取数据
    private void getData() {
        //1234567890016
        showCustomLoadBar(R.string.loading);
        String numbers = et_order_number.getText().toString().trim();
        RequestParams params = new RequestParams(Config.Api.express_receiver);
        if ("".equals(numbers) && numbers.length() <= 0) {
            toast("订单号不能为空");
            hideCustomLoadBar();
        } else {
            params.addBodyParameter("numbers", numbers);
            x.http().post(params, new Callback.CommonCallback<String>() {
                @Override
                public void onSuccess(String result) {
                    Dlog.e("GG快递代收返回result" + result);
                    int errCode = JsonUtils.getErrCode(result);
                    String errMsg = JsonUtils.getInfo(result, "errMsg");
                    String data = JsonUtils.getInfo(result, "data");
                    Dlog.e("GG快递代收返回data" + data);
                    if (errCode == 0) {
//                        com.alibaba.fastjson.JSONArray.parseArray(data,ExpressReceive.class);
                        ExpressReceive expressReceive = JSON.parseObject(data, ExpressReceive.class);
//                       expressReceiveList = (List<ExpressReceive>) JSON.parseObject(data, ExpressReceive.class);
                        expressListAdapter = new ExpressListAdapter(ExpressReceiveActivity.this, expressReceive);
                        lv_expressReceibver.setAdapter(expressListAdapter);
                    } else if (errCode == 4012) {
                        tv_no_info.setVisibility(View.VISIBLE);
                        tv_no_info.setText(errMsg);
                        lv_expressReceibver.setVisibility(View.GONE);
                    } else {
                        tv_no_info.setVisibility(View.VISIBLE);
                        lv_expressReceibver.setVisibility(View.GONE);
                    }
                }

                @Override
                public void onError(Throwable ex, boolean isOnCallback) {
                    hideCustomLoadBar();
                }

                @Override
                public void onCancelled(CancelledException cex) {

                }

                @Override
                public void onFinished() {
                    hideCustomLoadBar();
                }
            });
        }
    }

}

//            case R.id.ll_express_name:
//                ivArrow.setImageResource(R.drawable.arrow_up);
//                if (expressSelectPopupWindow != null && expressSelectPopupWindow.isShowing()) {
//                    expressSelectPopupWindow.dismiss();
//                } else {
//                    if (expressSelectPopupWindow == null) {
//                        expressSelectPopupWindow = new ExpressSelectPopupWindow(ExpressReceiveActivity.this, expressList, sectionSeclectListener);
//                    }
//                    expressSelectPopupWindow.showAsDropDown(rlTop);
//                }
//                expressSelectPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
//                    @Override
//                    public void onDismiss() {
//                        ivArrow.setImageResource(R.drawable.arrow_down);
//                    }
//                });
//                break;

//    AdapterView.OnItemClickListener sectionSeclectListener = new AdapterView.OnItemClickListener() {
//        @Override
//        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//            tvExpressName.setText(expressList.get(i));
//            expressSelectPopupWindow.dismiss();
//            ivArrow.setImageResource(R.drawable.arrow_down);
//            //TODO 重新获取快递列表
//        }
//    };
//    private void getData() {
//        expressReceiveList = new ArrayList<>();
//        for (int i = 1; i <= 10; i++) {
//            ExpressReceive expressReceive = new ExpressReceive();
//            expressReceive.setDate("2016-08-" + i);
//            expressReceive.setUser_name("王小" + i);
//            expressReceiveList.add(expressReceive);
//        }
//        expressListAdapter = new ExpressListAdapter(context, expressReceiveList);
//        lvExpress.setAdapter(expressListAdapter);
//    }

//    public List<String> getExpressList() {
//        expressList = new ArrayList<>();
//        expressList.add("顺丰");
//        expressList.add("圆通");
//        expressList.add("申通");
//        expressList.add("韵达");
//        expressList.add("中通");
//        return expressList;
//    }